package com.rm.zeyou.response;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.response.vo.MPageHelperVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

/**
 * 统一返回类
 *
 * @author YY
 * @date 2022/8/11
 */
@Data
@Log4j2
@ApiModel(value = "统一返回类")
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ApiRestResponse<T> {
    @ApiModelProperty(value = "响应码", example = "200", required = true)
    private String code;

    @ApiModelProperty(value = "响应信息", required = true)
    private String msg;

    @ApiModelProperty(value = "响应数据")
    private T data;

    private static final String OK_CODE = "200";

    private static final String OK_MSG = "OK";

    /**
     * 三个参数的构造函数（用于返回有数据的接口）
     */
    public ApiRestResponse(String status, String msg, T data) {
        this.code = status;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 两个参数的构造函数（用于返回没有数据，但要返回信息的接口）
     */
    public ApiRestResponse(String status, String msg) {
        this.code = status;
        this.msg = msg;
    }

    /**
     * 无参调用两个参数的构造函数（不用每次都编写返回码和返回信息，直接调用该方法）
     */
    public ApiRestResponse() {
        this(OK_CODE, OK_MSG);
    }

    /**
     * 返回一个通用的成功方法
     */
    public static <T> ApiRestResponse<T> success() {
        return new ApiRestResponse<>();
    }

    /**
     * 返回一个通用的成功方法并且有返回值的
     */
    public static <T> ApiRestResponse<T> success(T result) {
        ApiRestResponse<T> response = new ApiRestResponse<>();
        if (result instanceof PageInfo) {
            log.info("pagehelper分页返回");
            MPageHelperVo pageHelperVo = new MPageHelperVo();
            pageHelperVo.setPageNum(((PageInfo<?>) result).getPageNum());
            pageHelperVo.setPages(((PageInfo<?>) result).getPages());
            pageHelperVo.setPageSize(((PageInfo<?>) result).getPageSize());
            pageHelperVo.setTotal(((PageInfo<?>) result).getTotal());
            pageHelperVo.setData(((PageInfo<?>) result).getList());
            response.setData((T) pageHelperVo);
        } else if (result instanceof Page) {
            log.info("mybatisplus分页返回");
            MPageHelperVo pageHelperVo = new MPageHelperVo();
            pageHelperVo.setPageNum((int) ((Page<?>) result).getCurrent());
            pageHelperVo.setPageSize((int) ((Page<?>) result).getSize());
            pageHelperVo.setPages((int) ((Page<?>) result).getPages());
            pageHelperVo.setTotal(((Page<?>) result).getTotal());
            pageHelperVo.setData(((Page<?>) result).getRecords());
            response.setData((T) pageHelperVo);
        } else {
            response.setData(result);
        }
        return response;
    }

    /**
     * 返回一个通用的失败方法并且有返回值的
     */
    public static <T> ApiRestResponse<T> error(String code, String msg) {
        return new ApiRestResponse<>(code, msg);
    }


    public static <T> ApiRestResponse<T> error(ErrorCodeExceptionEnum errorCodeExceptionenum) {
        return new ApiRestResponse<>(errorCodeExceptionenum.getCode(), errorCodeExceptionenum.getMsg());
    }


}
